//
//  FWCHomeTableViewController.swift
//  微博项目001
//
//  Created by 樊武超 on 16/4/6.
//  Copyright © 2016年 樊武超. All rights reserved.
//

import UIKit

let cellid="cellid"

class FWCHomeTableViewController: FWCBasicTableViewController {
    
   lazy var userData:[FWCHomeDataModel]=[FWCHomeDataModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !isLogin {
        visterView!.setView("visitordiscover_feed_image_house", title: "关注一些人,回到这里看看有什么惊喜", isAnimation:true)
            return
        }
        navigationItem.leftBarButtonItem=UIBarButtonItem(imageName: "navigationbar_friendsearch", title: nil, target: self, selector: "push")
        navigationItem.rightBarButtonItem=UIBarButtonItem(imageName: "navigationbar_pop", title: nil, target: self, selector: "push")
        
        loadData()
        //注册cell
        tableView.registerClass(FWCHomeTableViewCell.self,forCellReuseIdentifier: cellid)
        
       
        
    }
    func push(){
        navigationController!.pushViewController(FWCViewController(), animated: true)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //数据源方法
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return userData.count
        }
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(cellid, forIndexPath: indexPath) as! FWCHomeTableViewCell

        let model = userData[indexPath.row]
        
            cell.states = model
//        cell.textLabel?.text = model.text
//        cell.textLabel?.textColor=UIColor.redColor()
        return cell
        }

       func loadData(){
        guard let token=FWCHomeViewModel.shareHomeViewModel.userMessage?.access_token else{
            print("数据加载错误")
            return
        }
       
       FWCNetworkTools.shareNetworkTool.loadHomePage(token) { (result, error) -> () in
        
        if error != nil {
        print("数据加载失败")
            return
        }
        print(result)
        
        guard let dict = result as? [String: AnyObject] else{
        
        print("数据格式不正确")
            return
        }
        guard let arras = dict["statuses"] as? [[String: AnyObject]] else{
        
        print("数据不正确")
            return
        }
        var tempArras = [FWCHomeDataModel]()
        
        for dict in arras {
        
            let model:FWCHomeDataModel = FWCHomeDataModel(dict: dict)
        
//            print(model)
            
            tempArras.append(model)
            
        }
        
        self.userData=tempArras
        
        self.tableView.reloadData()
        
        }
    
    }
   
}
